package com.eras.shoes.utils;

import com.eras.shoes.contents.WeatherApiService;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class WeatherRepository {
    private final WeatherApiService apiService;

    public WeatherRepository() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.openweathermap.org/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        apiService = retrofit.create(WeatherApiService.class);
    }

    public void getPressure(double lat, double lng, Callback<Double> callback) {
        apiService.getWeatherData(lat, lng, "YOUR_API_KEY", "metric")
                .enqueue(new retrofit2.Callback<WeatherApiService.WeatherResponse>() {
                    @Override
                    public void onResponse(Call<WeatherApiService.WeatherResponse> call, Response<WeatherApiService.WeatherResponse> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            callback.onSuccess(response.body().getMain().getPressure());
                        } else {
                            callback.onFailure("API error");
                        }
                    }

                    @Override
                    public void onFailure(Call<WeatherApiService.WeatherResponse> call, Throwable t) {
                        callback.onFailure(t.getMessage());
                    }
                });
    }

    public interface Callback<T> {
        void onSuccess(T result);
        void onFailure(String error);
    }
}

